<?php

/**
 * Controller_homework
 * 教师布置家庭作业
 * 
 */

class Controller_homework extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->semester  = YOU::loadModel('semester');

	}
    /** 首页 **/
    public function actionIndex()
    {

    }
	/** 家庭 **/
	public function actionList ()
	{	
		// 获取当前的本周开始时间结束时间
//		$start_time = $this->now_start;
//		$ent_time   =$this->now_end;
//
//		$where ="WHERE create_time>='{$start_time}' AND create_time<='{$ent_time}'";
        //var_dump($this->user);
        $type = $this->user['user_type'];
        if($type == 'staff'){
            $staff_id = $this->user['staff_id'];
            $where = " staff_id = {$staff_id} ";
            $this->tpl->assign('staff_id',$staff_id);
        }
        if($type == 'student'){
            $student_id = $this->user['student_id'];
            $row = $this->db->fetchRow("select `class_id` from `tbl_student` where `student_id` = {$student_id} ");
            $where = " class_id = {$row['class_id']} ";
        }
        if($type == 'admin'){
            $where = " 1 = 1 ";
        }

		// 分页（只展现一个周的内容）
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_homework` where {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);	
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$homework_lists = $this->db->fetchAll("SELECT * FROM `tbl_homework` WHERE {$where} ORDER BY homework_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($homework_lists as $key => $value){
			// 班级
			$class_data = $this->model->class->getInfo($value['class_id']);
			$homework_lists[$key]['class_name'] = $class_data['class_name'];
			// 学科
			$subject_data=$this->model->subject->getInfo($value['subject_id']);
			$homework_lists[$key]['subject_name'] = $subject_data['subject_name'];
			// 时间
			//$homework_lists[$key]['homework_date'] = date('Y-d-m',strtotime($value['create_time']));
		}
        //var_dump($homework_lists);
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		
		$this->tpl->assign("homework_lists", $homework_lists);
		$this->tpl->display('homework/homework.list.tpl.php');

	}
	/** 布置作业添加 **/
	public function actionAdd()
    {
        $semester_data = $this->model->semester->getNew();
        $semester_id   = $semester_data['semester_id'];
        if($this->user['user_type'] !='staff'){
            Helper_Client::alert("好好完成老师布置的作业哦");
        }
		// 所有学科
        $subject_lists = $this->db->fetchAll("SELECT DISTINCT b.* FROM tbl_staff_subject as a,tbl_subject as b
        where a.subject_id = b.subject_id and a.staff_id = {$this->user['type_id']}");
		//$subject_lists = $this->db->fetchAll("SELECT * FROM `tbl_subject`");
        //tbl_staff_subject
        $staff_id = $this->user['type_id'];
        $this->tpl->assign('staff_id',$staff_id);
		// 获得老师所分的班级
		$class_lists = $this->db->fetchAll("SELECT DISTINCT class_id FROM `tbl_staff_subject` WHERE staff_id='{$this->user['type_id']}' AND semester_id='{$semester_id}'");

        foreach ($class_lists as $key => $value){
			// 班级名称
			$row = $this->model->class->getinfo($value['class_id']);
			$class_lists[$key]['class_name'] = $row['class_name'];
		}//var_dump($class_lists);exit;
		// 获得老师教的班级所有的名字和id

		
        if ($_POST){
            $staff_id = $this->user['type_id'];
			$class_id				= $this->post->class_id;
			$subject_id				= $this->post->subject_id;
			$homework_content     	= $this->post->homework_content;
            $homework_date          = $this->post->homework_date;
			
			if(empty($class_id)){

				Helper_Client::alert("请选择班级");
			}
			if(empty($subject_id)){

				Helper_Client::alert("请选择学科");
			}

			//var_dump($class_id);exit;
			foreach($class_id as $key => $value){
				
				// 写入数据库
				$info = array(
			    "homework_date"     =>$homework_date,
                "staff_id"             =>$staff_id,
				"class_id"				=> $value,
				"subject_id"			=> $subject_id,
				"homework_content"		=> $homework_content,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",  
				
				);
				$this->db->do_insert("tbl_homework", $info);
			
			}
			
            			
			// 跳转
            Helper_Client::location("布置作业成功", "index.php?a=homework.List");
        }
       
      	$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->assign("class_lists", $class_lists);
        $this->tpl->display('homework/homework.add.tpl.php');
    }
	/** 布置作业修改 **/
    public function actionUpdate()
    {
        if($this->user['user_type'] !='staff'){
            Helper_Client::alert("好好完成老师布置的作业哦");
        }
        $homework_id   = (int)$this->get->homework_id;		
		$homework_data = $this->db->fetchRow("SELECT * FROM `tbl_homework` WHERE homework_id={$homework_id}");
        if($this->user['type_id'] && $this->user['type_id']!= $homework_data['staff_id']){
            Helper_Client::alert("无权限");
            exit;
        }
        $class_data = $this->model->class->getInfo($homework_data['class_id']);
        $homework_data['class_name'] = $class_data['class_name'];
        // 学科
        $subject_data=$this->model->subject->getInfo($homework_data['subject_id']);
        $homework_data['subject_name'] = $subject_data['subject_name'];

//var_dump($homework_data);

        // 权限老师id

		// 所有学科
	$subject_lists = $this->model->subject->getAll();
//		// 获得老师所分的班级
//		$subject_list = $this->db->fetchAll("SELECT * FROM `tbl_staff_subject` WHERE staff_id={$staff_id}");
//		// 获得老师教的班级所有的名字和id
//		foreach($subject_list as $key => $value){
//
//			 $class_name = $this->model->class->getInfo($value['class_id']);
//			 $class_lists[$key]['class_name'] = $class_name['class_name'];
//			 $class_lists[$key]['class_id'] = $class_name['class_id'];
//		}
        if ($_POST){
            $staff_id = $this->user['type_id'];
            $class_id				= $this->post->class_id;
            $subject_id				= $this->post->subject_id;
            $homework_content     	= $this->post->homework_content;
            $homework_date          = $this->post->homework_date;


            if(empty($subject_id)){

                Helper_Client::alert("请选择学科");
            }
 
            //var_dump($class_id);exit;


                // 写入数据库
                $info = array(
                    "homework_date"     =>$homework_date,
                    "staff_id"             =>$staff_id,
                    "subject_id"			=> $subject_id,
                    "homework_content"		=> $homework_content,
                    "status"                => 1,
                    "create_time"           => date("Y-m-d H:i:s"),
                    "update_time"           => "0000-00-00 00:00:00",

                );

                $this->db->do_update("tbl_homework", $info, "homework_id='{$homework_id}'");





            // 跳转
            Helper_Client::location("修改成功", "index.php?a=homework.list");
        }
	

        $this->tpl->assign('data',$homework_data);
		$this->tpl->assign("subject_lists", $subject_lists);
//		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("homework_data", $homework_data);
        $this->tpl->display('homework/homework.update.tpl.php');
    }


}
?>